<%@ page contentType="text/html;charset=UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<c:set var="extLibs" value="grid"/>
<sys:header title="商品管理" extLibs="${extLibs}" />
<div class="portlet box ${boxStyleColor}">
	<div class="portlet-title">
		<div class="caption"><i class="fa fa-coffee"></i> 商品管理</div>
		<div class="actions">
			<a href="#" class="btn btn-default btn-sm" id="btnSearch" title="查询"><i class="fa fa-search"></i> 查询</a>
			<shiro:hasPermission name="mdm:inventory:edit">
				<a href="${ctx}/mdm/inventory/form" class="btn btn-default btn-sm btnTool" title="新增商品"><i class="fa fa-plus"></i> 新增</a>
				<div class="btn-group">
					<a class="btn btn-default btn-sm" href="#" data-toggle="dropdown"><i class="fa fa-cogs"></i> 工具 <i class="fa fa-angle-down"></i></a>
					<ul class="dropdown-menu pull-right" style="width:100px;">
						<li><a href="#" id="btnExport" > <i class="fa fa-external-link"></i> 导出</a></li>
					    <li><a href="#"  id="btnImport" > <i class="fa fa-pencil-square-o"></i> 导入</a></li>
					    <li><a href="#"  id="btnPrint" > <i class="fa fa-print"></i> 打印</a></li>
					</ul>
		    </div>
		</div>
			</shiro:hasPermission>
		</div>
	</div>
	<div class="portlet-body list">
		<form:form id="searchForm" modelAttribute="inventory" action="${ctx}/mdm/inventory/listData" method="post" class="breadcrumb form-inline hide"
				data-page-no="${page.pageNo}" data-page-size="${page.pageSize}" data-order-by="${page.orderBy}">
			<div class="form-group">
				<label class="control-label">商品名称：</label>
				<div class="input-inline">
					<form:input path="invName" htmlEscape="false" maxlength="100" class="form-control input-sm width-160"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">品牌：</label>
				<form:select path="brandCode" class="form-control width-90 input-sm"  >
				    <form:option  value="">全部</form:option>
					<form:options items="${brandList}" itemLabel="brandName" itemValue="brandCode" htmlEscape="false"/>
				</form:select>
			</div>
			<div class="form-group">
				<label class="control-label">系列：</label>
				<form:select path="catenaCode" class="form-control width-90 input-sm"  >
				    <form:option  value="">全部</form:option>
					<form:options items="${catenaList}" itemLabel="catenaName" itemValue="catenaCode" htmlEscape="false"/>
				</form:select>
			</div>
			<div class="form-group">
				<label class="control-label">规格型号：</label>
				<div class="input-inline">
					<form:input path="invstd" htmlEscape="false" maxlength="10" class="form-control input-sm width-160"/>
				</div>
			</div>
			<div class="form-group" style="margin-left:10px;"> 
				<input class="btn btn-primary btn-sm" type="submit" value="查询"/>&nbsp;
				<input class="btn default btn-sm" type="reset" value="重置"/>
			</div>
		</form:form>
		<table id="dataGrid"></table>
	    <div id="dataGridPage"></div>
	</div>
</div>
<sys:footer extLibs="${extLibs}" />
<script type="text/javascript">
	// 初始化DataGrid对象
	$("#dataGrid").dataGrid({
		
		searchForm: $("#searchForm"), // 数据来源表单
		tabPageId: '${param.tabPageId}',// 当前页签编号
		showCheckbox:true,
		
		// 设置数据表格列
		columnModel: [
			{header:'商品编码', name:'invCode', index:'a.inv_code', width:120, align:"left", frozen:true, formatter: function(val, obj, row, act){
				return '<a href="${ctx}/mdm/inventory/form?invCode='+row.invCode+'" class="btnList" data-title="编辑商品">'+(val ? val : row.id)+'</a>';
			}},
			{header:'商品名称', name:'invName', index:'a.inv_name', width:200, align:"left"},
			{header:'分类', name:'sortName', index:'a.sort_name', width:120, align:"center"},
			{header:'品牌', name:'brandName', index:'a.brand_name', width:120, align:"center"},
			{header:'系列', name:'catenaName', index:'a.catena_name', width:120, align:"center"},
			{header:'规格型号', name:'invstd', index:'a.invstd', width:200, align:"left"},
			{header:'产地', name:'origine', index:'a.origine', width:200, align:"left",hidden:true},
			{header:'颜色', name:'color',  width:100, align:"right"},
			{header:'售价', name:'retailPrice', index:'a.retail_price', width:100, align:"right"},
			{header:'备注信息', name:'remarks', index:'a.remarks', width:300, align:"left"},
			{header:'更新时间', name:'updateDate', index:'a.update_date', width:180, align:"left"},
			{header:'操作', name:'actions', width:100, fixed:true, sortable:false, formatter: function(val, obj, row, act){
				var actions = [];//<shiro:hasPermission name="mdm:inventory:edit">
				if (row.status == Global.STATUS_NORMAL){
					actions.push('<a href="${ctx}/mdm/inventory/form?invCode='+row.invCode+'" class="btnList" title="编辑商品"><i class="fa fa-pencil"></i></a>&nbsp;');
					actions.push('<a href="${ctx}/mdm/inventory/delete?invCode='+row.invCode+'" class="btnList" title="删除该商品" data-confirm="确认要删除该商品吗？"><i class="fa fa-trash-o"></i></a>&nbsp;');
				}//</shiro:hasPermission>
				return actions.join('');
			}}
		],
		ajaxSuccess: function(data){ // 加载成功后执行方法
			
		}
	});
	
	$("#btnPrint").click(function(){
		
		var invCodes=$('#dataGrid').dataGrid('getSelectRows');
		if(invCodes.length==0){
			showMessage("请选择要打印的商品");
			return;
		}
		if(invCodes.length>4){
			showMessage("每张最多打印4个商品");
			return;
		}
	 	//console.log(invCodes);
	 	
	 	var url="${ctx}/mdm/inventory/printForm?printCodes="+invCodes;
	 	addTabPage('', '标签打印', url);
	});
	//导入
	$('#btnImport').click(function(){
		var url='${ctx}/mdm/inventory/import';
		addTabPage($(this), "商品导入", url, '${param.tabPageId}');
	});
	//导出
	$("#btnExport").click(function(){
		top.$.jBox.confirm("确认要导出数据吗？","系统提示",function(v,h,f){			
			if(v=="ok"){
				ajaxDownload('${ctx}/mdm/inventory/export',$("#searchForm").serialize());
			}
		},{buttonsFocus:1});
		top.$('.jbox-body .jbox-icon').css('top','55px');
	});
	
	/**
	 * Ajax 文件下载   ajaxDownload('ExcelDownload.do','find=commoncode','post' );
	 * @param url
	 * @param data
	 * @param method
	 */
	 function ajaxDownload(url, data, method) {
			// 获取url和data
			if (url && data) {
				// data 是 string 或者 array/object
				data = typeof data == 'string' ? data : jQuery.param(data);
				// 把参数组装成 form的 input
				var inputs = '';
				$.each(data.split('&'), function() {
					var pair = this.split('=');
					inputs += '<input type="hidden" name="' + pair[0] + '" value="'
							+ pair[1] + '" />';
				});
				// request发送请求
				$('<form action="' + url + '" method="' + (method || 'post')
						+ '" >' + inputs + '</form>').appendTo('body').submit().remove();
			}
		}
	
</script>